<script setup lang="ts">
import { getPatientList } from '@/services/user'
import type { Patient, PatientList } from '@/types/user'
import { onMounted, ref, computed } from 'vue'

const show = ref(false)

const list = ref<PatientList>([])

import { useRouter } from 'vue-router'
import { useConsultStore } from '@/stores'
const store = useConsultStore()
const router = useRouter()
const next = async () => {
  if (!patientId.value) return showToast('请选就诊择患者')
  store.setPatient(patientId.value)
  router.push('/consult/pay')
}

const loadList = async () => {
  const res = await getPatientList()
  list.value = res.data

  // 当是选择患者且有患者信息的时候，设置默认选中的 ID，
  if (isChange.value && list.value.length) {
    // 从 list.value 中找 defaultFlag 为 1 的对象
    const defPatient = list.value.find((item) => item.defaultFlag === 1)
    // 能找到，就把找到的 id 给 patientId
    if (defPatient) patientId.value = defPatient.id
    // 否者默认选中第一项
    else patientId.value = list.value[0].id
  }
}
onMounted(() => {
  loadList()
})

import { useRoute } from 'vue-router'
import { showToast } from 'vant'
// 是否是选择患者
const route = useRoute()
const isChange = computed(() => route.query.isChange === '1')

const patientId = ref<string>()
const selectedPatient = (item: Patient) => {
  if (isChange.value) {
    patientId.value = item.id
  }
}
</script>

<template>
  <div class="patient-page">
    <cp-nav-bar :title="isChange ? '选择患者' : '家庭档案'"></cp-nav-bar>
    <!-- 如果是选择患者显示【头部提示】 -->
    <div class="patient-change" v-if="isChange">
      <h3>请选择患者信息</h3>
      <p>以便医生给出更准确的治疗，信息仅医生可见</p>
    </div>
    <div class="patient-list">
      <div
        class="patient-item"
        v-for="item in list"
        @click="selectedPatient(item)"
        :class="{ selected: patientId === item.id }"
        :key="item.id"
      >
        <div class="info">
          <span class="name">{{ item.name }}</span>
          <!-- ?: 不作为匹配项，所以 $2 就是 (.{4}) 这个分组 -->
          <span class="id">{{ item.idCard.replace(/^(.{6})(?:\d+)(.{4})$/, '$1******$2') }}</span>
          <span>{{ item.genderValue }}</span>
          <span>{{ item.age }}岁</span>
        </div>
        <div class="icon">
          <cp-icon name="user-edit" />
        </div>
        <div class="tag" v-if="item.defaultFlag === 1">默认</div>
      </div>
      <div class="patient-add" v-if="list.length < 6">
        <cp-icon name="user-add" />
        <p>添加患者</p>
      </div>
      <!-- 如果是选择患者显示【底部按钮】 -->
      <div class="patient-next" v-if="isChange" @click="next">
        <van-button type="primary" round block>下一步</van-button>
      </div>
    </div>

    <!-- 侧边栏 -->
    <van-popup v-model:show="show" position="right">
      <cp-nav-bar title="添加患者" right-text="保存"></cp-nav-bar>
    </van-popup>
  </div>
</template>

<style lang="scss" scoped>
.patient-change {
  padding: 15px;
  > h3 {
    font-weight: normal;
    margin-bottom: 5px;
  }
  > p {
    color: var(--cp-text3);
  }
}
.patient-next {
  padding: 30px;
  background-color: #fff;
  position: fixed;
  left: 0;
  bottom: 0;
  width: 100%;
  height: 80px;
  box-sizing: border-box;
}

.patient-page {
  padding: 46px 0 80px;
  ::v-deep() {
    .van-popup {
      width: 100%;
      height: 100%;
      padding-top: 46px;
      box-sizing: border-box;
    }
  }
}

.patient-list {
  padding: 15px;
}

.patient-item {
  display: flex;
  align-items: center;
  padding: 15px;
  background-color: var(--cp-bg);
  border-radius: 8px;
  margin-bottom: 15px;
  position: relative;
  border: 1px solid var(--cp-bg);
  transition: all 0.3s;
  overflow: hidden;

  .info {
    display: flex;
    flex-wrap: wrap;
    flex: 1;

    span {
      color: var(--cp-tip);
      margin-right: 20px;
      line-height: 30px;

      &.name {
        font-size: 16px;
        color: var(--cp-text1);
        width: 80px;
        margin-right: 0;
      }

      &.id {
        color: var(--cp-text2);
        width: 180px;
      }
    }
  }

  .icon {
    color: var(--cp-tag);
    width: 20px;
    text-align: center;
  }

  .tag {
    position: absolute;
    right: 60px;
    top: 21px;
    width: 30px;
    height: 16px;
    font-size: 10px;
    color: #fff;
    background-color: var(--cp-primary);
    border-radius: 2px;
    display: flex;
    justify-content: center;
    align-items: center;
  }

  &.selected {
    border-color: var(--cp-primary);
    background-color: var(--cp-plain);

    .icon {
      color: var(--cp-primary);
    }
  }
}

.patient-add {
  background-color: var(--cp-bg);
  color: var(--cp-primary);
  text-align: center;
  padding: 15px 0;
  border-radius: 8px;

  .cp-icon {
    font-size: 24px;
  }
}

.patient-tip {
  color: var(--cp-tag);
  padding: 12px 0;
}
</style>
